/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.dataaccess.dao.user.dao;

import com.nus.dataaccess.dao.base.dao.BaseDao;
import com.nus.dataaccess.dao.user.UserInfo;
import java.util.List;
import java.util.Map;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author robin.foe
 */
public class UserInfoDao extends BaseDao<UserInfo>{

    private static UserInfoDao instance;

    public static UserInfoDao getInstance() {
        if (null == instance) {
            instance = new UserInfoDao();
        }
        return instance;
    }
    @Override
    protected Class getReferenceClass() {return UserInfo.class;}

    public UserInfo getUserInfoById(Long userInfoId){
       Criteria crit = super.createCriteria();
       crit.add(Restrictions.eq("id", userInfoId));
       crit.setMaxResults(1);
       List<UserInfo> userInfos = crit.list();
       return (userInfos!=null && !userInfos.isEmpty()) ? userInfos.get(0) : null;
    }
    public int getTotalCount(Map<String, String> filters){
        Criteria crit = super.createCriteria();
        crit.setProjection(Projections.rowCount());
        return ((Long)crit.list().get(0)).intValue();
    }
    public List<UserInfo> paginate(int first, int pageSize, String sortField, boolean ascending, Map<String, String> filters){
        Criteria crit = super.createCriteria();

        crit.setFirstResult(first);
        crit.setMaxResults(pageSize);
        return crit.list();
    }

}
